﻿Imports System.Data.OleDb
Public Class frmThemMucTruHoaDonTheoKhachHang
#Region "Form Event"
    Public MucTruHoaDonTheoKhachHangId As Integer = -1
    Private Sub frmThemMucTruHoaDon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If MucTruHoaDonTheoKhachHangId <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsNhanVienTiepThi As New DataSet
                Dim dtNhanVienTiepThi As New DataTable
                Dim query As String = "Select MT.*,KH.TenKhachHang From MucTruHoaDonTheoKhachHang As MT" & _
                                    "   Left Outer Join DanhMucKhachHang As KH On MT.KhachHangId=KH.KhachHangId" & _
                                    "   Where MT.Xoa= 0 And MT.MucTruHoaDonTheoKhachHangId=" & MucTruHoaDonTheoKhachHangId
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsNhanVienTiepThi)
                dbConn.Close()
                dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
                txtKhoanTruHoaDon.Text = dtNhanVienTiepThi.Rows(0)("TenMucTru")
                If dtNhanVienTiepThi.Rows(0)("TheoPhanTram") = "Yes" Then
                    cbxIsPercent.Checked = True
                Else
                    cbxIsPercent.Checked = False
                End If
                txtTru.Text = dtNhanVienTiepThi.Rows(0)("Tru")
                txtGhiChu.Text = dtNhanVienTiepThi.Rows(0)("GhiChu")
                txtKhachHang.Text = dtNhanVienTiepThi.Rows(0)("TenKhachHang")
                Label1.Text = dtNhanVienTiepThi.Rows(0)("KhachHangId")
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Thêm Khách Hàng"
    Sub KhachHang_Quick_Search()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsKhachHang As New DataSet
            Dim dtKhachHang As New DataTable
            Dim query As String = "Select KhachHangId,MaKhachHang,TenKhachHang,SoDienThoai,DiaChi From DanhMucKhachHang Where TenKhachHang Like N'%" & txtKhachHang.Text.Trim & "%'"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsKhachHang)
            dbConn.Close()
            dtKhachHang = dsKhachHang.Tables(0)
            Panel_KhachHang.Visible = True
            dgv_KhachHang_QuickSearch.AutoGenerateColumns = False
            dgv_KhachHang_QuickSearch.DataSource = dtKhachHang
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    Private Sub dgv_KhachHang_QuickSearch_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_KhachHang_QuickSearch.CellDoubleClick
        Try
            'Gán ID Khách Hàng
            Label1.Text = dgv_KhachHang_QuickSearch.Rows(dgv_KhachHang_QuickSearch.CurrentCell.RowIndex).Cells("KhachHangId_Search").Value.ToString()
            txtKhachHang.Text = dgv_KhachHang_QuickSearch.Rows(dgv_KhachHang_QuickSearch.CurrentCell.RowIndex).Cells("TenKhachHang_QS").Value.ToString()
            Panel_KhachHang.Visible = False
            cmdSave.Focus()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub txtKhachHang_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKhachHang.KeyUp
        Try
            If txtKhachHang.Text <> "" Then
                KhachHang_Quick_Search()
            Else
                Panel_KhachHang.Visible = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Function IsNotExist() As Boolean
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsKhachHang As New DataSet
            Dim dtKhachHang As New DataTable
            Dim query As String = "Select Count(*) From DanhMucKhachHang Where LOWER(TenKhachHang)=N'" & txtKhachHang.Text.ToLower & "'"
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDb.OleDbCommand(query, dbConn)
            Dim iResult As Integer
            iResult = dbCommand.ExecuteScalar()
            If iResult = 0 Then
                Return True
            Else
                Return False
            End If
            dbConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Panel_KhachHang.Visible = False
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Button Event"

    Private Sub txtKhoanTruHoaDon_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKhoanTruHoaDon.KeyUp
        Try
            txtKhoanTruHoaDon.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtTru_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtTru_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            'If Not cbxIsPercent.Checked Then
            '    If txtTru.Text <> "" Then
            '        txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
            '        txtTru.Text = CDec(txtTru.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
            '    End If
            'Else
            '    txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
            'End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub txtTru_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            'txtTru.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtKhoanTruHoaDon.Text = "" Then
                MessageBox.Show("Bạn chưa nhập tên khoản trừ hóa đơn.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtKhoanTruHoaDon.Focus()
                Exit Sub
            End If
            If txtTru.Text = "" Then
                MessageBox.Show("Bạn chưa nhập khoản trừ hóa đơn.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtKhoanTruHoaDon.Focus()
                Exit Sub
            End If
            If cbxIsPercent.Checked Then
                If Single.Parse(txtTru.Text) > 100 Then
                    MessageBox.Show("Khoản trừ phần trăm hóa đơn không hợp lý.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If
            End If
            If IsNotExist() Then
                MessageBox.Show("Tên khách hàng bạn chọn chưa có trong danh mục.Vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtKhachHang.Focus()
                Exit Sub
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            Dim TheoPhanTram As String
            If cbxIsPercent.Checked Then
                TheoPhanTram = "Yes"
            Else
                TheoPhanTram = "No"
            End If
            Dim IdKhachHang As String = Label1.Text
            If MucTruHoaDonTheoKhachHangId = -1 Then
                query = String.Format("Insert Into MucTruHoaDonTheoKhachHang(TenMucTru,Tru,TheoPhanTram,GhiChu,KhachHangId,NgayTao,NguoiTao) Values(N'{0}',{1},N'{2}',N'{3}',{4},N'{5}',N'{6}')", txtKhoanTruHoaDon.Text, Single.Parse(txtTru.Text.Trim, Globalization.NumberStyles.Currency), TheoPhanTram, txtGhiChu.Text, IdKhachHang, NgayTao, NguoiTao)
            Else
                query = String.Format("Update MucTruHoaDonTheoKhachHang Set TenMucTru=N'{0}',Tru={1},TheoPhanTram=N'{2}',GhiChu=N'{3}', KhachHangId={4}, NgaySuaGanNhat=N'{5}',NguoiSuaGanNhat=N'{6}' Where MucTruHoaDonTheoKhachHangId={7}", txtKhoanTruHoaDon.Text, Single.Parse(txtTru.Text, Globalization.NumberStyles.Currency), TheoPhanTram, txtGhiChu.Text, IdKhachHang, NgayTao, NguoiTao, MucTruHoaDonTheoKhachHangId)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtKhoanTruHoaDon.Text = ""
            txtTru.Text = "0.00"
            txtGhiChu.Text = ""
            If MucTruHoaDonTheoKhachHangId <> -1 Then
                Label1.Text = "0"
                txtKhachHang.Text = ""
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtKhoanTruHoaDon.Text = ""
            txtTru.Text = "0.00"
            txtGhiChu.Text = ""
            Label1.Text = "0"
            txtKhachHang.Text = ""
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"

#End Region

    Private Sub cbxIsPercent_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxIsPercent.CheckedChanged
        'If Not cbxIsPercent.Checked Then
        '    '--->

        '    If txtTru.Text <> "" Then
        '        'txtTru.Text = txtTru.Text.Split(",")(0)
        '        'txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
        '        txtTru.Text = CDec(txtTru.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
        '    End If
        'Else
        '    '--->Theo phần trăm
        '    txtTru.Text = txtTru.Text.Split(",")(0).Replace(".", "")
        'End If
    End Sub

    
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Panel_KhachHang.Visible = False
    End Sub
End Class